<template>
  <div class="container">
    <get-user-info v-if="isGetUserInfo" pageType="getCard" @updateUserInfo="initEls"></get-user-info>
  </div>
</template>

<script>
import { parseShortCode, getAppInfo } from '@/api/common'
import GetUserInfo from '@/components/GetUserInfo'
import { getAuth } from '@/api/login'
import { getMemberInfo } from '@/api/home'
import {
  setToken,
  setCustomerId,
  setPhone,
  setMemberId,
  setEnterpriseId,
  setAppInfo,
  setVeekrayStatus,
  setShowBuyCard
} from '@/utils/auth'
export default {
  components: { GetUserInfo },
  data() {
    return {
      isComponents: true,
      paramsData: {},
      resType: '',
      resArrTxt: [],
      resTitle: '',
      resText: '',
      appInfo: {},
      scanCode: {},
      isGetUserInfo: false
    }
  },
  onLoad(options) {
    this.initEls(options)
  },
  methods: {
    initEls(options) {
      console.log('扫小程序码进入')
      this.$pt.setStorageSync('scanIn', true)
      if (this.$pt.getStorageSync('scanCode')) {
        this.scanCode = this.$pt.getStorageSync('scanCode')
        if (this.scanCode && !options.scene) {
          options.scene = this.scanCode.scene
        }
      }
      if (options.scene) {
        this.scanCode.scene = options.scene
        // 扫小程序码进入
        let code = decodeURIComponent(options.scene)
        let params = {}
        getAppInfo().then((response) => {
          if (response.isError) return
          this.appInfo = response.data
          setAppInfo(this.appInfo)
          if (this.appInfo.defaultMemberId) {
            setMemberId(this.appInfo.defaultMemberId)
            this.$utils.setMyId(this.appInfo.defaultMemberId)
          }
          if (this.appInfo.enterpriseId) {
            setEnterpriseId(this.appInfo.enterpriseId)
          }
          parseShortCode(code).then((response) => {
            if (response.isError) {
              this.$utils.showToast({ title: '扫码失败！' })
              this.paramsData = params
              if (this.getCustomerId) {
                this.$pt.reLaunch({ url: '/pages/home/main' })
              } else {
                this.wxLogin(this.paramsData)
              }
              return
            }
            params = response.data
            console.log('params:', params)
            // 商品详情
            if (params.path === 'pages/shop/detail/main') {
              this.paramsData = params
              this.paramsData.scene = options.scene
              this.$pt.removeStorageSync('scanCode')
              if (this.getCustomerId) {
                this.$pt.reLaunch({ url: '/pages/shop/detail/main?goodsId=' + params.goodsId + '&goodsType=' + params.goodsType })
              } else {
                this.$pt.setStorageSync('scanCode', this.paramsData)
                this.wxLogin(this.paramsData)
              }
            }
            // 主播招募员
            if (params.path === 'pages/recruit_anchor_code/main') {
              this.paramsData = params
              this.paramsData.scene = options.scene
              this.$pt.removeStorageSync('scanCode')
              if (this.getCustomerId) {
                this.$pt.reLaunch({ url: '/pages/personal/apply-anchor/main?params=' + JSON.stringify(params) + '&inPage=1' })
              } else {
                this.$pt.setStorageSync('scanCode', this.paramsData)
                this.wxLogin(this.paramsData)
              }
            }
            // 主播合伙人
            if (params.path === 'pages/partner_anchor_code/main') {
              this.paramsData = params
              this.paramsData.scene = options.scene
              this.$pt.removeStorageSync('scanCode')
              if (this.getCustomerId) {
                this.$pt.reLaunch({ url: '/pages/personal/apply-anchor/main?params=' + JSON.stringify(params) + '&inPage=1' })
              } else {
                this.$pt.setStorageSync('scanCode', this.paramsData)
                this.wxLogin(this.paramsData)
              }
            }
            // 总部招募码
            if (params.path === 'pages/enterprise_anchor_code/main') {
              this.paramsData = params
              this.paramsData.scene = options.scene
              this.$pt.removeStorageSync('scanCode')
              if (this.getCustomerId) {
                this.$pt.reLaunch({ url: '/pages/personal/apply-anchor/main?params=' + JSON.stringify(params) + '&inPage=1' })
              } else {
                this.$pt.setStorageSync('scanCode', this.paramsData)
                this.wxLogin(this.paramsData)
              }
            }
            // 主播小程序
            if (params.path === 'pages/anchor_code/main') {
              this.paramsData = params
              this.paramsData.scene = options.scene
              this.$pt.removeStorageSync('scanCode')
              if (this.getCustomerId) {
                this.$pt.reLaunch({ url: '/pages/personal/apply-anchor/main?params=' + JSON.stringify(params) + '&inPage=1' })
              } else {
                this.$pt.setStorageSync('scanCode', this.paramsData)
                this.wxLogin(this.paramsData)
              }
            }
            // 主播预览码
            if (params.path === 'pages/home/custom-preview/main') {
              this.paramsData = params
              this.paramsData.scene = options.scene
              this.$pt.removeStorageSync('scanCode')
              if (this.getCustomerId) {
                this.customPreview()
              } else {
                this.$pt.setStorageSync('scanCode', this.paramsData)
                this.wxLogin(this.paramsData)
              }
            }
            // 主播首页码
            if (params.path === 'pages/home/main') {
              this.paramsData = params
              this.paramsData.scene = options.scene
              this.$pt.removeStorageSync('scanCode')
              if (this.getCustomerId) {
                this.$pt.reLaunch({ url: '/pages/home/main' })
              } else {
                this.$pt.setStorageSync('scanCode', this.paramsData)
                this.wxLogin(this.paramsData)
              }
            }
          })
        })
      }
    },
    wxLogin(obj) {
      const that = this
      that.$utils.showToast({
        title: '加载中',
        icon: 'loading',
        duration: 3000
      })
      that.$pt.login({
        success(res) {
          if (res.code) {
            console.log('employee-login:' + res.code)
            getAuth(res.code).then((response) => {
              if (response.isError) return
              setToken(response.data.token)
              if (!response.data.customerId) {
                console.log('!customerId')
                // that.$utils.showToast({ title: '请先授权，再进行绑定！' })
                that.isGetUserInfo = true
                return
              }
              setCustomerId(response.data.customerId)
              if (response.data.phone) {
                setPhone(response.data.phone)
              }
              if (response.data.showBuyCard) {
                setShowBuyCard(response.data.showBuyCard)
              }
              // 关闭loading
              that.$pt.hideToast()
              // 判断是否商品详情
              if (obj.path === 'pages/shop/detail/main') {
                that.$pt.reLaunch({ url: '/pages/shop/detail/main?goodsId=' + obj.goodsId + '&goodsType=' + obj.goodsType })
              }
              // 判断是否主播招募员
              if (obj.path === 'pages/recruit_anchor_code/main') {
                that.$pt.reLaunch({ url: '/pages/personal/apply-anchor/main?params=' + JSON.stringify(obj) + '&inPage=1' })
              }
              // 判断是否主播合伙人
              if (obj.path === 'pages/partner_anchor_code/main') {
                that.$pt.reLaunch({ url: '/pages/personal/apply-anchor/main?params=' + JSON.stringify(obj) + '&inPage=1' })
              }
              // 判断是否总部招募码
              if (obj.path === 'pages/enterprise_anchor_code/main') {
                that.$pt.reLaunch({ url: '/pages/personal/apply-anchor/main?params=' + JSON.stringify(obj) + '&inPage=1' })
              }
              // 判断是否主播小程序
              if (obj.path === 'pages/anchor_code/main') {
                that.$pt.reLaunch({ url: '/pages/personal/apply-anchor/main?params=' + JSON.stringify(obj) + '&inPage=1' })
              }
              // 判断是否主播预览码
              if (obj.path === 'pages/home/custom-preview/main') {
                that.customPreview()
              }
              // 判断是否主播首页
              if (obj.path === 'pages/home/main') {
                that.$pt.reLaunch({ url: '/pages/home/main' })
              }
            }).catch(() => {
            })
          } else {
            that.$utils.showToast({ title: '登录失败' })
          }
        }
      })
    },
    // 自定义预览页面
    customPreview() {
      if (!this.getCustomerId) {
        setTimeout(() => {
          this.$pt.reLaunch({
            url: '/pages/home/main'
          })
        }, 300)
        return
      }
      getMemberInfo(this.getCustomerId).then((response) => {
        if (response.isError) return
        if (response.data.veekrayStatus === 1) {
          setMemberId(this.getCustomerId)
          this.$utils.setMyId(this.getCustomerId)
          setVeekrayStatus(response.data.veekrayStatus)
        } else {
          setMemberId(this.appInfo.defaultMemberId)
          this.$utils.setMyId(this.appInfo.defaultMemberId)
        }
        this.$pt.removeStorageSync('scanCode')
        let that = this
        setTimeout(() => {
          that.$pt.reLaunch({
            url: '/pages/home/custom-preview/main?params=' + JSON.stringify(that.paramsData)
          })
        }, 800)
      })
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  display: flex;
  justify-content: center;
  background: #ffffff;
  min-height: calc(100vh);
}
</style>

